/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;



/**
 *
 * @author spring
 */
public class PlotPanelPOI extends javax.swing.JPanel implements ComponentListener {
    POIBut[] pb = new POIBut[20];
    DVBut[] db = new DVBut[1500];
    int npoi = 0;
    int ndocs = 0;
    int mag = 50;
    
 
    public PlotPanelPOI(){
        super();
        
    }
    
    
    @Override
public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2 = (Graphics2D)g;
    int h = this.getHeight();
    int w = this.getWidth();
    g.drawLine(0, h/2, w, h/2);
    g.drawLine(w/2,0, w/2, h);
    for (int i=0;i<ndocs;i++)
    if (db[i].showAttMat){
        double[] am = db[i].getAttractMatrix();
        java.awt.Point p1 = db[i].getLocation();
        for (int j=0; j<npoi ;j++){
            if (pb[j].isActive()){
              java.awt.Point p2 = pb[j].getLocation();
                if (am[pb[j].getNum()]>0.0){
                    int width = (int)(am[pb[j].getNum()]*1000.0);
                    //System.out.println("width="+width+", "+am[pb[j].getNum()]);
                    g2.setStroke(new BasicStroke(width));
                    g2.drawLine(p1.x+10,p1.y+15,p2.x+30,p2.y+30);
                }
           }
        }    
    }
}

    @Override
    public void removeAll() {
        super.removeAll();
        npoi=0;
        ndocs=0;
    }
   
public void intializePOIPanel(){
    int ncomp=this.getComponentCount();
    java.awt.Component[] cc = getComponents();
    for (int i=0; i<ncomp ;i++){
            if (cc[i].getClass().getName().contains("POIBut")){
              pb[npoi] =(POIBut)cc[i];
              this.setComponentZOrder(pb[npoi], 1);
             // System.out.println("POIVEC"+npoi+" = "+pb[npoi].getToolTipText()+printdvec(pb[npoi].getdoubleVector()));
              npoi++;
            }
            if (cc[i].getClass().getName().contains("DVBut")){
              db[ndocs] =(DVBut)cc[i]; 
              this.setComponentZOrder(db[ndocs], 0);
              ndocs++;
            }
        }
}
    
 public void replotDB(){
    int x = this.getWidth();
    int y = this.getHeight();
    double dx= (double)x;
    double dy= (double)y;
    for (int i=0; i<ndocs ;i++){
        double[] loc = new double[2];
        double di= (double)i;
        loc[0]=0.0;
        loc[1]=0.0;
        for (int j =0;j<npoi;j++){
            if (pb[j].isActive()){
               double a = getAttraction(db[i].getdoubleVector(),pb[j].getdoubleVector());
               double[] p = pb[j].getcoords();
               loc[0]+=a*p[0]*mag;
               loc[1]+=a*p[1]*mag;
            }
        }
        if (loc[0]>.95) loc[0]= .95+(0.1*Math.random()-.05);
        if (loc[0]<-.95) loc[0]= -.95+(0.1*Math.random()-.05);
        if (loc[1]>.95) loc[1]= .95+(0.1*Math.random()-.05);
        if (loc[1]<-.95) loc[1]= -.95+(0.1*Math.random()-.05);
        if (loc[0]==0.0 && loc[1]==0.0 ){
            loc[0]=-1.0+((double)i*3.0/(double)x);
            loc[1]=-1.0;
        }
        db[i].setcoords(loc[0], loc[1]);       
    }
this.repaint();
}
    
public void componentHidden(ComponentEvent e) {
}

public void componentMoved(ComponentEvent e) {
}
public void setMag(int m){
        mag=m;
    }
    
public void componentResized(ComponentEvent e) {
    Component c = e.getComponent();
    int ncomp=((Container)e.getSource()).getComponentCount();
    for (int i=0; i<ncomp ;i++){
        if (((Container)e.getSource()).getComponent(i).getClass().getName().contains("POIBut")){
            POIBut x =(POIBut)((Container)e.getSource()).getComponent(i);
            x.parentresize(c.getSize().width, c.getSize().height);   
        }
        else{
            DVBut y =(DVBut)((Container)e.getSource()).getComponent(i);
            y.parentresize(c.getSize().width, c.getSize().height);
        }
    }
}

public void componentShown(ComponentEvent e) {
}

private double getAttraction(double[] document, double[] poi) {
    double attraction = 0.0;
    double factor = (double) document.length;
    for (int i = 0; i < document.length; i++) {
        if (document[i]>0 && poi[i]>0)
        attraction += (document[i]*poi[i])/factor;
   }
   return attraction;
}
    
private double getSimDistance(double[] scores1,double[] scores2){
    double sum = 0.0;
    for (int i=0;i<scores1.length; i++){
        sum += Math.pow((scores2[i]-scores1[i]), 2.0);
    }
    double distance = Math.sqrt(sum);
    return 1.0-(1/(1+distance));
}  

 private String printdvec(double[] a) {
        String matrix = new String("{");
        for (int i = 0; i < a.length; i++) {
            matrix += a[i] + ",";
        }
        return matrix.substring(0, matrix.length() - 1) + "}";
    }

}
